package com.hermit.qmst.UI.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hermit.qmst.R;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.RequestHttp;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by Administrator on 2017/1/7 0007.
 */
@ContentView(R.layout.new_activity_up_pwd)
public class UppasswordActivity extends Activity implements View.OnClickListener {
    @ViewInject(R.id.iv_back)
    ImageView mIvBack;
    @ViewInject(R.id.tv_title)
    TextView mTvTitle;
    @ViewInject(R.id.et_name)
    EditText mEtName;
    @ViewInject(R.id.iv_name)
    ImageView mIvName;
    @ViewInject(R.id.et_pwd)
    EditText mEtPwd;
    @ViewInject(R.id.et_code)
    EditText et_code;
    @ViewInject(R.id.iv_pwd)
    ImageView mIvPwd;
    @ViewInject(R.id.bt_save)
    Button mBtSave;
    private boolean ismEtPwd = false;
    private boolean ismEtName = false;
    private String upPwdUrl = Common.mUrl + "szdh/resetPassword.php?username=";
    private String pwd = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        mIvBack.setOnClickListener(this);
        mBtSave.setOnClickListener(this);
        mIvName.setOnClickListener(this);
        mIvPwd.setOnClickListener(this);
        mTvTitle.setText("修改密码");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_pwd:
                if (!ismEtPwd) {
                    mEtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    mEtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                ismEtPwd = !ismEtPwd;
                mEtPwd.postInvalidate();
                CharSequence text = mEtPwd.getText();
                if (text instanceof Spannable) {
                    Spannable spanText = (Spannable) text;
                    Selection.setSelection(spanText, text.length());
                }
                break;
            case R.id.iv_name:
                if (!ismEtName) {
                    mEtName.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    mEtName.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                ismEtName = !ismEtName;
                mEtName.postInvalidate();
                CharSequence text1 = mEtPwd.getText();
                if (text1 instanceof Spannable) {
                    Spannable spanText = (Spannable) text1;
                    Selection.setSelection(spanText, text1.length());
                }
                break;
            case R.id.bt_save:
                pwd = mEtPwd.getText().toString();
                if (pwd.length() >= 6) {
                    if (mEtName.getText().toString().equals(pwd)) {
                        save();
                    } else {
                        Toast.makeText(UppasswordActivity.this, "两次输入的密码不一样", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(UppasswordActivity.this, "密码太简单了，请输入6位及6位以上数密码", Toast.LENGTH_SHORT).show();
                }

                break;
        }

    }

    private void save() {
        String acctName = "";
        try {
            acctName = URLEncoder.encode(et_code.getText().toString(), "utf-8");
        } catch (UnsupportedEncodingException e) {

        }
        String url = upPwdUrl + VerifyPhoneActivity.phone + "&password=" + pwd + "&acct=" + acctName;
        RequestHttp.getHttp(this, url, new RequestHttp.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject) {
                try {
                    if (jsonObject.getBoolean("state")) {
                        startActivity(new Intent(UppasswordActivity.this, LoginActivity.class));
                        Toast.makeText(UppasswordActivity.this, "修改密码成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(UppasswordActivity.this, "修改密码失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
